{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "flush-broadcasting",
   "metadata": {},
   "source": [
    "https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list\n",
    "\n",
    "\n",
    "Runtime: 48 ms, faster than 22.06% of Python3 online submissions for Flatten a Multilevel Doubly Linked List.\n",
    "Memory Usage: 15.1 MB, less than 29.91% of Python3 online submissions for Flatten a Multilevel Doubly Linked List.\n",
    "\n",
    "\n",
    "```python\n",
    "class Solution:\n",
    "    def flatten(self, head: 'Node') -> 'Node':\n",
    "        #11:19\n",
    "        l = [head]\n",
    "        newL = []\n",
    "        while len(l):\n",
    "            node = l[0]\n",
    "            l = l[1:]\n",
    "            if node:\n",
    "                if node.next != None:\n",
    "                    l.insert(0, node.next)\n",
    "                if node.child != None:\n",
    "                    l.insert(0, node.child)\n",
    "                newL.append(node)\n",
    "        for i, v in enumerate(newL):\n",
    "            #print(v.val)\n",
    "            if i == 0:\n",
    "                newL[i].prev = None\n",
    "                newL[i].next = newL[i+1]\n",
    "            elif i == len(newL) - 1:\n",
    "                newL[i].prev = newL[i-1]\n",
    "                newL[i].next = None\n",
    "            else:\n",
    "                newL[i].prev = newL[i-1]\n",
    "                newL[i].next = newL[i+1]\n",
    "            newL[i].child = None\n",
    "        if len(newL):\n",
    "            return newL[0]\n",
    "        else:\n",
    "            return head\n",
    "        #11:21 done; 11:32 debug            \n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "perceived-worry",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
